package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.base.BaseActivity;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.SignupActivity;
import com.example.yuejian.yuejian.bean.UsersInfo;
import com.example.yuejian.yuejian.model.ActivityModel;
import com.example.yuejian.yuejian.model.UserModel;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ActivitySignActivity extends BaseActivity {
    private TextView activityname;
    private TextView activitymoney;
    private TextView username;
    private TextView userphone;
    private TextView safe_username;
    private TextView user_idCard;
    private EditText et_num;
    private ImageButton num_add;
    private ImageButton num_reduce;
    private RadioButton rb_zhifubao;
    private RadioButton rb_wechat;
    private TextView all_money;
    private Button pay_btn;
    private ImageView btn_back;

    String active_name;
    String active_money;//单价
    int num=1;//初始数量
int money;
    int sum;//总数
   double totalmoney;
    String pay_way="支付宝";
    String createtime;
    String activityid;

    private ModleListener<UsersInfo> userinfoListener=new ModleListener<UsersInfo>() {
        @Override
        public void onResponse(UsersInfo o) {
            UsersInfo res=o;


            username.setText(res.getNickname());
            userphone.setText(res.getPhone());
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplication(),msg,Toast.LENGTH_SHORT).show();
        }
    };
    private  ModleListener<SignupActivity>signupListener=new ModleListener<SignupActivity>() {
        @Override
        public void onResponse(SignupActivity o) {
            SignupActivity res=o;
            if (0==res.getRetCode())
            {
                Toast.makeText(getApplication(),res.getRetMsg(),Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(getApplication(),res.getRetMsg(),Toast.LENGTH_SHORT).show();
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplication(),"未知错误",Toast.LENGTH_SHORT).show();
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        initViews();
        getActvityinfo();
        getuserDatas();
        getData();
        initEvents();

    }
    private void initViews()
    {
        activityname=(TextView)findViewById(R.id.activity_sign_activityname);
        activitymoney=(TextView)findViewById(R.id.activity_sign_activitymoney);
        username=(TextView)findViewById(R.id.activity_sign_name);
        userphone=(TextView)findViewById(R.id.activity_sign_phone);
        safe_username=(TextView)findViewById(R.id.activity_sign_et_username);
        user_idCard=(TextView)findViewById(R.id.activity_sign_idCad);
        et_num=(EditText) findViewById(R.id.avtivity_sign_buy_num);
        num_add=(ImageButton) findViewById(R.id.activity_imgBtn_up);
        num_reduce=(ImageButton) findViewById(R.id.activity_imgBtn_down);
        rb_zhifubao=(RadioButton) findViewById(R.id.activity_pay_type_zhifubao);
        rb_wechat=(RadioButton) findViewById(R.id.activity_pay_type_wechat);
        all_money=(TextView) findViewById(R.id.activity_sign_all_money);
        pay_btn=(Button) findViewById(R.id.activity_sign_pay_btn);
    }
    private void getActvityinfo()
    {
        Intent intent=getIntent();
        active_name=intent.getStringExtra("activity_name");
         active_money=intent.getStringExtra("money");
        activityid=intent.getStringExtra("aid");
        activityname.setText(active_name);
        activitymoney.setText(active_money+"/人");
    }
    private void  getuserDatas()
    {
        UserModel userModel=new UserModel();
        userModel.userInfo(getUser_id(),userinfoListener);
    }
    private  void getData(){

        money=Integer.valueOf(active_money);
        all_money.setText("¥"+active_money);

       /* SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd");
        dff.setTimeZone(TimeZone.getTimeZone("GMT+08"));
        String ee = dff.format(new Date());*/

       SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
         createtime = formatter.format(curDate);
        //Toast.makeText(getApplication(),createtime,Toast.LENGTH_SHORT).show();
        et_num.setText(num+"");
    }
    private void initEvents()
    {
        rb_zhifubao.setOnClickListener(onClickListener);
        rb_wechat.setOnClickListener(onClickListener);
        num_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String n=et_num.getText().toString();
                et_num.setText(String.valueOf(Integer.parseInt(n)+1));
                int sum=   Integer.valueOf(et_num.getText().toString());
                totalmoney=money*sum;
                all_money.setText("¥"+String.valueOf(totalmoney));
            }
        });
        num_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n=et_num.getText().toString();
                int nn=Integer.parseInt(n)-1;
                et_num.setText(String.valueOf(nn<1?1:nn));
                sum= Integer.valueOf(et_num.getText().toString());
                totalmoney=money*sum;
                all_money.setText(String.valueOf(totalmoney));
            }
        });
        pay_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityModel model=new ActivityModel();
                model.signupActivity(activityid,getUser_id(),sum,safe_username.getText().toString(),user_idCard.getText().toString(),
                        totalmoney,pay_way,signupListener);
            }
        });
    }

    View.OnClickListener  onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.activity_pay_type_zhifubao:
                    pay_way="支付宝";
                    Toast.makeText(ActivitySignActivity.this, pay_way, Toast.LENGTH_SHORT).show();

                    break;
                case R.id.activity_pay_type_wechat:
                    pay_way="微信";
                    Toast.makeText(ActivitySignActivity.this, pay_way, Toast.LENGTH_SHORT).show();

                    break;
            }

        }
    };

}
